﻿using System;

// Load the configuration from the 'web.config' file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace Noria.Web.Utility
{
    public class Global : System.Web.HttpApplication
    {
        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private Authenticator auther = new Authenticator();
        private ErrorHandler handler = new ErrorHandler();

        protected void Application_Start(object sender, EventArgs e)
        {
            log.Info("Application_Start");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            log.DebugFormat("Application_BeginRequest -> [{0}] {1}", ClientHelper.GetIPAddress(Context.Request), Context.Request.Url);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            log.Debug("Application_AuthenticateRequest -> " + Context.Request.Url);
            auther.Authenticate(Context.Request);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            handler.Process(Context.Response, Context.Server.GetLastError());
            Context.Server.ClearError();
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            log.Info("Application_End");
        }
    }
}